﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using WPFExcelReport.Helpers; 
using System.Windows;
  
namespace WPFExcelReport
{
    public partial class VABPOS
    {
      
        void DoYGRMUZTWork()
        {
            if (IsRun)
            {
                return;
            }
            IsRun = true;
            string path = @"C:\IntexReport\Data\YGRMU";
            string ITXDBVERSION = "YGR";
            YGRMUZT(ITXDBVERSION, path);
        }
        public void YGRMUZT(string ITXDBVERSION, string path)
        {
            try
            {
                List<DataRow> list;
                while (true)
                {
                    DataSet SetJob = GetJob(path);
                    if (SetJob.Tables.Count == 0)
                    {
                        IsRun = false;
                        break;
                    }
                    list = (List<DataRow>)CreateAB50(ITXDBVERSION, SetJob);
                    if (list == null)
                        return;
                    string KRS = "", LFD = "", POS = "", POSTYP = "";
                    string ABKRS = "", ABLFD = "", ABPOS = "";
                    foreach (DataRow row in list)
                    {
                        if (row.Table.TableName == "ABPOS")
                        {
                            KRS = row["ABPAUFKRS"].ToString();
                            LFD = row["ABPAUFLFD"].ToString();
                            POS = row["ABPAUFPOS"].ToString();
                            POSTYP = row["ABPPOSTYP"].ToString();
                        }
                    }
                    DataRow dwData = QTB.TBMain.NewRow();
                    if (POSTYP == "HX ")
                    {

                        var qMU = from DataRow row in SetJob.Tables["ICC_ABAX"].AsEnumerable()
                                  where row["ABAAUFKRS"].ToString() == KRS
                                  && row["ABAAUFLFD"].ToString() == LFD
                                  && row["ABAAUFPOS"].ToString() == POS
                                  select row;
                        foreach (DataRow row in qMU)
                        {
                            dwData["GAMANDANT"] = app.Mandant;
                            dwData["GAAUFKRS"] = row["ABAVKAUFKRS"];
                            dwData["GAAUFLFD"] = row["ABAVKAUFLFD"];
                            dwData["GAAUFPOS"] = row["ABAVKAUFPOS"];
                            dwData["GASA"] = "713";
                            INTEXDBC.UpdateGAAUF(dwData, 18);//去除回修标志
                        }
                        return;
                    }
                    dwData = QTB.TBMain.NewRow();
                    var qABA = from DataRow row in SetJob.Tables["ICC_ABAX"].AsEnumerable()
                               where row["ABAAUFKRS"].ToString() == KRS
                               && row["ABAAUFLFD"].ToString() == LFD
                               && row["ABAAUFPOS"].ToString() == POS
                               select row;
                    foreach (DataRow row in qABA)
                    {
                        ABKRS = row["ABAVKAUFKRS"].ToString();
                        ABLFD = row["ABAVKAUFLFD"].ToString();
                        ABPOS = row["ABAVKAUFPOS"].ToString();
                        dwData["GAMANDANT"] = app.Mandant;
                        dwData["GAAUFKRS"] = row["ABAVKAUFKRS"];
                        dwData["GAAUFLFD"] = row["ABAVKAUFLFD"];
                        dwData["GAAUFPOS"] = row["ABAVKAUFPOS"];
                        dwData["GASA"] = "713";
                        INTEXDBC.UpdateGAAUF(dwData, 3);
                        PM["KRS"] = row["ABAVKAUFKRS"];
                        PM["LFD"] = row["ABAVKAUFLFD"];
                        PM["POS"] = row["ABAVKAUFPOS"];
                        ZU.RVZUTEI(PM);
                        INTEXDBC.DeleteICC_ABAX(row, 6);
                        dwData["ABPMANDANT"] = app.Mandant; ;
                        dwData["ABPAUFKRS"] = row["ABAVKAUFKRS"];
                        dwData["ABPAUFLFD"] = row["ABAVKAUFLFD"];
                        dwData["ABPAUFPOS"] = row["ABAVKAUFPOS"];
                        INTEXDBC.UpdateABPOS(dwData, 6);
                    }
                    dwData["GAAUFKRS"] = KRS;
                    dwData["GAAUFLFD"] = LFD;
                    INTEXDBC.UpdateGAAUF(dwData, 5);
                    dwData = VGD.TBMain.NewRow();
                    dwData["GAMANDANT"] = app.Mandant;
                    dwData["GAAUFKRS"] = ABKRS;
                    dwData["GAAUFLFD"] = ABLFD;
                    dwData["GAAUFPOS"] = ABPOS;
                    dwData["GASA"] = "713";
                    SResult = VGD.QGD29(dwData);
                    DataTable tbGD = SResult.Tables[0].Copy();
                    dwData = TBMain.NewRow();
                    dwData["ABPMANDANT"] = app.Mandant;
                    dwData["ABPAUFKRS"] = KRS;
                    dwData["ABPAUFLFD"] = LFD; 
                    SResult = QABP49(dwData);
              
                    DataTable tbABP = SResult.Tables[0].Copy();
                    if (tbGD.Rows.Count != tbABP.Rows.Count)
                    { 
                        throw new Exception("分盘结果异常,请联系管理人员");
                   
                    }
                   

                }
                EventDispatcher.Instance.RaiseStatus("分盘完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                MessageBox.Show("分盘结果异常,请联系管理人员");
                EventDispatcher.Instance.RaiseStatus("分盘异常终止", StatusEventType.StopProgress);
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                IsRun = false;
            }
        }
        public object CreateAB50(string ITXDBVERSION, DataSet db)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            List<DataRow> list = null; ;
            string filter = "";
            DataRow[] rows;
            DataRow dwTemp = null;
            string KRS = "", LFD = "", POS = "", ABPNEUBEN, ABPNEUDAT;
            decimal ABPSOLLMENGE1 = 0, ABPSOLLMENGE2 = 0, ABPSOLLMENGE3 = 0, ABPSOLLMENGE4 = 0, ABPSOLLMENGE5 = 0, ABPSOLLMENGE6 = 0, ABPSOLLMENGE7 = 0,
         ABPSOLLMENGE8 = 0;
            DataTable tbMU = db.Tables["ICC_ABAX"];
            DataTable tbABP = db.Tables["ABPOS_EXT"];
            int idx = 0;
            string LastKRS = "";
            var qABP = from DataRow row in tbABP.AsEnumerable()
                       where row["ABPAUFART"].ToString() == "GP"
                       select row;
            foreach (DataRow dwABP in qABP)
            {
                list = new List<DataRow>();

                ABPNEUBEN = dwABP["ABPNEUBEN"].ToString();
                ABPNEUDAT = dwABP["ABPNEUDAT"].ToString();
                string ABPSTARTDAT = dwABP["ABPSTARTDAT"].ToString();
                string ABPTERMDAT = dwABP["ABPTERMDAT"].ToString();
                KRS = dwABP["ABPAUFKRS"].ToString();
                if (LastKRS != KRS)
                {
                    LastKRS = KRS;
                    LFD = GetABP(ITXDBVERSION, KRS, ref db, dwABP);
                    list.Add(CreateABK(dwABP, ITXDBVERSION));
                }
                if (LFD == "" || LFD == "000000")
                {
                    throw new Exception("未能成功获取生产单号码:" + KRS);
                }
                idx++;
                POS = Helper.Right("000" + dwABP["ABPAUFPOS"].ToString(), 3);
                DataRow dwData = QTB.TBMain.NewRow();
                dwData["ABPMANDANT"] = app.Mandant;
                dwData["ABPAUFKRS"] = KRS;
                dwData["ABPAUFLFD"] = LFD;
                dwData["ABPAUFPOS"] = POS;
                SResult = INTEXDBC.QueryABPOS(dwData, 30);
                if (SResult.Tables[0].Rows.Count > 0)
                {
                    throw new Exception("生产单号码已经存在，请重新进行计划，并验证生产单号码，若有异常，立即通知管理员");
                }


                ABPSOLLMENGE1 = Convert.ToDecimal(dwABP["ABPSOLLMENGE1"].ToString());
                ABPSOLLMENGE2 = Convert.ToDecimal(dwABP["ABPSOLLMENGE2"].ToString());
                ABPSOLLMENGE3 = Convert.ToDecimal(dwABP["ABPSOLLMENGE3"].ToString());
                ABPSOLLMENGE4 = Convert.ToDecimal(dwABP["ABPSOLLMENGE4"].ToString());
                ABPSOLLMENGE5 = Convert.ToDecimal(dwABP["ABPSOLLMENGE5"].ToString());
                ABPSOLLMENGE6 = Convert.ToDecimal(dwABP["ABPSOLLMENGE6"].ToString());
                ABPSOLLMENGE7 = Convert.ToDecimal(dwABP["ABPSOLLMENGE7"].ToString());
                ABPSOLLMENGE8 = Convert.ToDecimal(dwABP["ABPSOLLMENGE8"].ToString());
                #region=======Reserved============
                bool IsReserved = false;
                if (tbMU != null)
                {
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "' AND ABAVKAUFLFD<>'X'";
                    rows = tbMU.Select(filter);
                    if (rows.Length > 0)
                    {
                        IsReserved = true;
                    }
                }
                #endregion
                #region=====ABPOS=========
                EventDispatcher.Instance.RaiseStatus("转换ABPOS:" + "-" + KRS + "-" + LFD + "-" + POS, StatusEventType.RunningProgress);
                DataRow dwABPOS = INTEXDBC.Convert2ABPOS(dwABP, ITXDBVERSION);
                dwABPOS["ABPMANDANT"] = app.Mandant; ;
                dwABPOS["ABPAUFKRS"] = KRS;
                dwABPOS["ABPAUFLFD"] = LFD;
                dwABPOS["ABPAUFPOS"] = POS;
                dwABPOS["ABPAUFART"] = dwABP["ABPAUFART"];
                dwABPOS["ABPDIVARTKZ"] = "N";
                dwABPOS["ABPSME"] = 5;
                dwABPOS["ABPWBSTA"] = "68";
                dwABPOS["ABPWSSTA"] = "20";
                dwABPOS["ABPSZUSATZKZ1"] = " ";
                dwABPOS["ABPZUTART"] = " ";
                dwABPOS["ABPFA"] = "0";
                dwABPOS["ABPGA"] = 0;
                dwABPOS["ABPQA"] = 0;

                dwABPOS["ABPWAKZ"] = dwABP["ABPWAKZ"];
                dwABPOS["ABPART"] = dwABP["ABPART"];
                dwABPOS["ABPVAR"] = dwABP["ABPVAR"];
                dwABPOS["ABPFARBE"] = dwABP["ABPFARBE"];
                dwABPOS["ABPZLO"] = dwABP["ABPZLO"];
                dwABPOS["ABPZLB"] = dwABP["ABPZLB"];
                dwABPOS["ABPSOLLMENGE1"] = dwABP["ABPSOLLMENGE1"];
                dwABPOS["ABPSOLLMENGE2"] = dwABP["ABPSOLLMENGE2"];
                dwABPOS["ABPSOLLMENGE3"] = dwABP["ABPSOLLMENGE3"];
                dwABPOS["ABPSOLLMENGE4"] = dwABP["ABPSOLLMENGE4"];
                dwABPOS["ABPSOLLMENGE5"] = dwABP["ABPSOLLMENGE5"];
                dwABPOS["ABPSOLLMENGE6"] = dwABP["ABPSOLLMENGE6"];
                dwABPOS["ABPSOLLMENGE7"] = dwABP["ABPSOLLMENGE7"];
                dwABPOS["ABPSOLLMENGE8"] = dwABP["ABPSOLLMENGE8"];

                dwABPOS["ABPZUGKG"] = dwABP["ABPZUGKG"];
                dwABPOS["ABPLIEFDAT"] = dwABP["ABPLIEFDAT"];
                dwABPOS["ABPVTDATE"] = dwABP["ABPVTDATE"];
                dwABPOS["ABPSTARTDAT"] = dwABP["ABPSTARTDAT"];
                dwABPOS["ABPENDDAT"] = dwABP["ABPENDDAT"];
                dwABPOS["ABPSTARTDATA"] = dwABP["ABPSTARTDATA"];
                dwABPOS["ABPENDDATA"] = dwABP["ABPENDDATA"];
                dwABPOS["ABPANRKR"] = dwABP["ABPANRKR"];
                dwABPOS["ABPALFDNR"] = dwABP["ABPALFDNR"];
                dwABPOS["ABPLIEFDATN"] = dwABP["ABPLIEFDATN"];
                dwABPOS["ABPPOSTYP"] = dwABP["ABPPOSTYP"];
                dwABPOS["ABPTERMDAT"] = dwABP["ABPTERMDAT"];
                dwABPOS["ABPSTARTDATN"] = dwABP["ABPSTARTDATN"];
                dwABPOS["ABPNEUBEN"] = dwABP["ABPNEUBEN"];
                dwABPOS["ABPNEUDAT"] = dwABP["ABPNEUDAT"];
                if (dwABP.Table.Columns.Contains("ABPSTKLG"))
                {
                    dwABPOS["ABPSTKLG"] = dwABP["ABPSTKLG"];
                }
                else
                {
                    dwABPOS["ABPSTKLG"] = 0;
                }
                list.Add(dwABPOS);

                #endregion
                #region=====ABPR=========
                list.Add(CreateABR(dwABPOS, ITXDBVERSION));
                #endregion
                #region====ABAUFM=========
                EventDispatcher.Instance.RaiseStatus("转换:ABAUFM" + "-" + KRS + "-" + LFD + "-" + POS, StatusEventType.RunningProgress);
                DataRow dwProd = INTEXDBC.Convert2ABAUFM(null, ITXDBVERSION, false);
                dwProd["ABAMANDANT"] = app.Mandant; ;
                dwProd["ABAAUFTYP"] = "V";
                dwProd["ABAAUFKRS"] = KRS;
                dwProd["ABAAUFLFD"] = LFD;
                dwProd["ABAAUFPOS"] = POS;
                dwProd["ABAAPOS"] = "0001";
                dwProd["ABASGESGEW"] = ABPSOLLMENGE5 * 10;
                if (IsReserved)
                {
                    dwProd["ABARSTSGEW"] = 0;
                }
                else
                {
                    dwProd["ABARSTSGEW"] = ABPSOLLMENGE5 * 10;
                }
                dwProd["ABALEAPOS"] = "2";
                dwProd["ABAVAKZ"] = " ";
                dwProd["ABALIEFKOMP"] = " ";
                dwProd["ABAASSCHNIT"] = "N";

                dwProd["ABANEUBEN"] = ABPNEUBEN;
                dwProd["ABANEUDAT"] = ABPNEUDAT;
                if (tbMU != null)
                {
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "' AND ABAVKAUFLFD<>'X'";
                    rows = tbMU.Select(filter);
                    foreach (DataRow dwReq in rows)
                    {
                        dwProd["ABAKDPARTIE"] = Helper.Left(dwReq["ABACOLORREF"].ToString().PadRight(10), 10);
                        break;
                    }
                }
                list.Add(dwProd);
                //Set4ITX.Merge(SResult);dwMU["ABAKDPARTIE"]
                if (tbMU != null)
                {
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "' AND ABAVKAUFLFD<>'X'";
                    rows = tbMU.Select(filter);
                    int ABAAPOS = 2;
                    foreach (DataRow dwReq in rows)
                    {
                        NewReservaton(dwProd, dwReq, ITXDBVERSION, "AB50MU01", ref list, ABAAPOS);
                        ABAAPOS++;
                    }
                }
                #endregion
                #region=====ABFOLGE=========
                ConsoldateFlow(dwABPOS, ITXDBVERSION, "AB50", ref list);
                #endregion
                #region=====GAAUF310===========
                EventDispatcher.Instance.RaiseStatus("转换GAAUF:" + "-" + KRS + "-" + LFD + "-" + POS, StatusEventType.RunningProgress);
                DataRow dwGAAUF = INTEXDBC.Convert2GAAUF(null, ITXDBVERSION, false);
                dwGAAUF["GAMANDANT"] = app.Mandant; ;
                dwGAAUF["GAAUFKRS"] = KRS;
                dwGAAUF["GAAUFLFD"] = LFD;
                dwGAAUF["GAAUFPOS"] = POS;
                dwGAAUF["GAPOS"] = "00";
                dwGAAUF["GASA"] = "310";
                dwGAAUF["GANEUBEN"] = ABPNEUBEN;
                dwGAAUF["GANEUDAT"] = ABPNEUDAT;
                dwGAAUF["GADATVON"] = ABPNEUDAT;
                dwGAAUF["GADATVON"] = dwABP["ABPSTARTDAT"];
                dwGAAUF["GADATBIS"] = dwABP["ABPSTARTDAT"];
                dwGAAUF["GAART"] = dwABP["ABPART"];
                dwGAAUF["GAVAR"] = dwABP["ABPVAR"];
                dwGAAUF["GAFARBE"] = dwABP["ABPFARBE"];

                dwGAAUF["GAKG1"] = ABPSOLLMENGE5;
                dwGAAUF["GAKG2"] = 0;
                if (IsReserved)
                {
                    dwGAAUF["GAKG2"] = ABPSOLLMENGE5;
                }
                dwGAAUF["GADISPOKZ"] = " ";
                dwGAAUF["GALO"] = "  ";
                dwGAAUF["GALB"] = "  ";
                dwGAAUF["GALP"] = "      ";
                dwGAAUF["GASPUL"] = 0;
                dwGAAUF["GALAENGE"] = 0;
                dwGAAUF["GASPULGEW"] = 0;
                dwGAAUF["GAAUFTRKZ"] = " ";
                dwGAAUF["GABASMG2"] = 0;

                list.Add(dwGAAUF);//310
                #endregion
                #region=====GAAUF===========
                filter = " ABAVKAUFTYP='V' AND ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";
                rows = tbMU.Select(filter);
                foreach (DataRow row in rows)
                {
                    dwGAAUF = INTEXDBC.Convert2GAAUF(null, ITXDBVERSION, false);
                    dwGAAUF["GAMANDANT"] = app.Mandant; ;
                    dwGAAUF["GAAUFKRS"] = KRS;
                    dwGAAUF["GAAUFLFD"] = LFD;
                    dwGAAUF["GAAUFPOS"] = POS;
                    dwGAAUF["GAPOS"] = row["ABAVKUPOS"];
                    dwGAAUF["GASA"] = "713";
                    dwGAAUF["GAAUFTRKZ"] = "G";
                    dwGAAUF["GAURVAR"] = " ";
                    dwGAAUF["GANEUBEN"] = ABPNEUBEN;
                    dwGAAUF["GANEUDAT"] = ABPNEUDAT;
                    dwGAAUF["GADATVON"] = ABPNEUDAT;
                    dwGAAUF["GADATBIS"] = ABPNEUDAT;
                    dwGAAUF["GAART"] = row["ABAART"];
                    dwGAAUF["GAVAR"] = row["ABAVAR"];
                    dwGAAUF["GAFARBE"] = row["ABAFARBE"];
                    dwGAAUF["GADISPOKZ"] = row["ABADISPOKZ"];
                    dwGAAUF["GAAUFMSL"] = row["ABAAUFMSL"];

                    dwGAAUF["GAKG1"] = Convert.ToDouble(row["ABARESMG"].ToString()) / 10;
                    dwGAAUF["GAKG3"] = Convert.ToDouble(row["ABAQTYALLOCATE"].ToString()) / 10;
                    dwGAAUF["GAKG5"] = Convert.ToDouble(row["ABAQTYRECEIVED"].ToString()) / 10;
                    dwGAAUF["GALO"] = row["ABALO"];
                    dwGAAUF["GALB"] = row["ABALB"];
                    dwGAAUF["GALP"] = row["ABALP"];
                    dwGAAUF["GASPUL"] = 0;
                    dwGAAUF["GALAENGE"] = 0;
                    dwGAAUF["GASPULGEW"] = 0;
                    string ABAVKFARBE = row["ABAVKFARBE"].ToString().Trim();
                    if (ABAVKFARBE == "")
                    {
                        dwGAAUF["GABASMG2"] = "100000";
                    }
                    else
                    {
                        dwGAAUF["GABASMG2"] = ABAVKFARBE;
                    }
                    list.Add(dwGAAUF);
                }
                #endregion
                #region=====ZUTEI===========
                if (db.Tables.Contains("ZUTEI"))
                {
                    EventDispatcher.Instance.RaiseStatus("转换:ZUTEI" + "-" + KRS + "-" + LFD + "-" + POS, StatusEventType.RunningProgress);
                    var qZU = from DataRow row in db.Tables["ZUTEI"].AsEnumerable()
                              where row["ZUAUFKRS"].ToString() == KRS
                              && row["ZUAUFLFD"].ToString() == LFD
                              && row["ZUAUFPOS"].ToString() == POS
                              select row;
                    foreach (DataRow row in qZU)
                    {
                        DataRow dwZU = INTEXDBC.Convert2ZUTEI(null, ITXDBVERSION, false);
                        dwZU["ZUMANDANT"] = row["ZUMANDANT"];
                        dwZU["ZUAUFKRS"] = row["ZUAUFKRS"];
                        dwZU["ZUAUFLFD"] = row["ZUAUFLFD"];
                        dwZU["ZUAUFPOS"] = row["ZUAUFPOS"];
                        dwZU["ZUSA"] = row["ZUSA"];
                        dwZU["ZUTEILNR"] = row["ZUTEILNR"];
                        dwZU["ZUPOS"] = row["ZUPOS"];
                        dwZU["ZUPARTIE"] = row["ZUPARTIE"];
                        dwZU["ZUPARTIEKZ"] = row["ZUPARTIEKZ"];
                        dwZU["ZULO"] = row["ZULO"];
                        dwZU["ZULB"] = row["ZULB"];
                        dwZU["ZULBEHI"] = row["ZULBEHI"];
                        dwZU["ZUAENDZL"] = row["ZUAENDZL"];
                        dwZU["ZUAENDBEN"] = row["ZUAENDBEN"];
                        dwZU["ZUAENDDAT"] = row["ZUAENDDAT"];
                        dwZU["ZUNEUBEN"] = row["ZUNEUBEN"];
                        dwZU["ZUNEUDAT"] = row["ZUNEUDAT"];
                        dwZU["ZUEINTKG"] = row["ZUEINTKG"];
                        dwZU["ZUEINTSP"] = row["ZUEINTSP"];
                        dwZU["ZUEINTKA"] = row["ZUEINTKA"];
                        dwZU["ZUENTNKG"] = row["ZUENTNKG"];
                        dwZU["ZUENTNSP"] = row["ZUENTNSP"];
                        dwZU["ZUENTNKA"] = row["ZUENTNKA"];
                        dwZU["ZULIEFZWP"] = row["ZULIEFZWP"];
                        dwZU["ZUSPPART"] = row["ZUSPPART"];
                        dwZU["ZUART"] = row["ZUART"];
                        dwZU["ZUVAR"] = row["ZUVAR"];
                        dwZU["ZUFARBE"] = row["ZUFARBE"];
                        dwZU["ZUDEZI"] = row["ZUDEZI"];
                        dwZU["ZUME"] = row["ZUME"];
                        dwZU["ZUERLKZ"] = row["ZUERLKZ"];
                        dwZU["ZUIKOND"] = row["ZUIKOND"];
                        dwZU["ZUFLAG"] = row["ZUFLAG"];
                        dwZU["ZUNTAUSCH"] = row["ZUNTAUSCH"];
                        dwZU["ZUFILLER"] = row["ZUFILLER"];
                        list.Add(dwZU);
                    }
                }
                #endregion
                #region===ICC_ABAX=============
                if (tbMU != null)
                {
                    filter = "ABAVKAUFKRS='" + KRS + "' AND ABAVKAUFLFD='" + LFD + "' AND ABAVKAUFPOS='" + POS + "'";

                    rows = tbMU.Select(filter);
                    if (rows.Length > 0)
                    {
                        foreach (DataRow row in rows)
                        {
                            DataRow dwABAX = INTEXDBC.Convert2ICC_ABAX(row, app.company, true);
                            dwABAX["ABAID"] = 0;
                            list.Add(dwABAX);
                        }
                    }
                    filter = "ABAAUFKRS='" + KRS + "' AND ABAAUFLFD='" + LFD + "' AND ABAAUFPOS='" + POS + "'";
                    rows = tbMU.Select(filter);
                    if (rows.Length > 0)
                    {
                        foreach (DataRow row in rows)
                        {
                            DataRow dwABAX = INTEXDBC.Convert2ICC_ABAX(row, app.company, true);
                            dwABAX["ABAID"] = 0;
                            list.Add(dwABAX);
                        }
                    }
                }
                #endregion
                SaveAB(ITXDBVERSION, list);
            }
            return list;
        }

        ///创建每个原料的染色生产单，并预留
        ///由显示GAAUF转变为显示ABAUFM的清单
        ///这里的主要作用是产生一个ABPOS，这个会用来传给最终的生产单创建程序，必要的信息要都保护
        ///在生产单真正创建过程中，将不在处理业务逻辑，相当于达到了intex的AB50/AB10界面的对于数据的要求
        ///表达好生产单之间的关系
        public DataSet PlanFPAB50(DataSet src, DataRow dwABA, ref int _ABAID)
        {
            string DEND = "0"; int DAYS = 0;
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = src.Tables["ABPOS"].Clone();
            string ENDD = "0";
            string STARTD = "0";
            string ART = "", VAR = "", FARBE = "", WAKZ = "";
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataTable tb;
            decimal RQTY = 0;
            string VKAUFKRS = "", VKAUFLFD = "", VKAUFPOS = "", ABAVKUPOS = "";
            DataTable tbMU = src.Tables["ICC_ABAX"];
            DataTable tbZU = src.Tables["ZUTEI_ITX"];

            #region========Prepare=================
            VKAUFKRS = dwABA["ABAVKAUFKRS"].ToString();
            VKAUFLFD = dwABA["ABAVKAUFLFD"].ToString();
            VKAUFPOS = dwABA["ABAVKAUFPOS"].ToString();
            ABAVKUPOS = dwABA["ABAVKUPOS"].ToString();
            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();
            APZANRKRS = "FP";
            APZALFDNR = "000002";
            RQTY = Convert.ToDecimal(dwABA["ABARESMG"].ToString());
            if (DEND == "0")
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DEND;
            }
            STARTD = Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - DAYS));
            if (STARTD.Trim() == "") STARTD = "0";
            #endregion

            #region=======dwABPN==========
            bool IsRepaire = false;
            if (dwABA["ABACOLORREF"].ToString().Trim() == "*HX*")
            {
                IsRepaire = true;
            }
            dwABPN = tmpABPOS.Rows.Add();
            dwABPN["ABPMANDANT"] = app.Mandant;
            dwABPN["ABPAUFART"] = "GP";
            dwABPN["ABPAUFKRS"] = dwABA["ABAAUFKRS"].ToString();
            dwABPN["ABPAUFLFD"] = "X";
            dwABPN["ABPAUFPOS"] = Helper.Right("000" + ABAVKUPOS, 3);
            dwABPN["ABPLTVORL"] = Helper.Left(dwABA["EXART"].ToString().PadRight(15), 15);
            dwABPN["ABPZLO"] = "01";
            dwABPN["ABPZLB"] = "HM";
            dwABPN["ABPBLO"] = "01";
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPPOSTYP"] = "MU ";
            dwABPN["ABPWAKZ"] = "G";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPVTDATE"] = ENDD;
            dwABPN["ABPENDDAT"] = ENDD;
            dwABPN["ABPENDDATA"] = ENDD;
            dwABPN["ABPLIEFDATN"] = ENDD;
            dwABPN["ABPTERMDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDATA"] = STARTD;
            dwABPN["ABPSTARTDATN"] = STARTD;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSOLLMENGE1"] = 0;
            dwABPN["ABPSOLLMENGE2"] = 0;
            dwABPN["ABPSOLLMENGE3"] = 0;
            dwABPN["ABPSOLLMENGE4"] = 0;
            dwABPN["ABPSOLLMENGE5"] = RQTY / 10;
            dwABPN["ABPSOLLMENGE6"] = 0;
            dwABPN["ABPSOLLMENGE7"] = 0;
            dwABPN["ABPSOLLMENGE8"] = 0;
            dwABPN["ABPZUGKG"] = RQTY / 10;
            if (IsRepaire)
            {
                dwABPN["ABPANRKR"] = "HX";
                dwABPN["ABPALFDNR"] = "000001";
                dwABPN["ABPAUFLFD"] = dwABA["ABAAUFLFD"].ToString();
                dwABPN["ABPAUFPOS"] = "9" + Helper.Right("00" + ABAVKUPOS, 2);
                dwABPN["ABPPOSTYP"] = "HX ";
                dwABPN["ABPSOLLMENGE7"] = dwABPN["ABPSOLLMENGE5"];
                dwABPN["ABPZUGKG"] = 0;
            }

            //dwABPN["ABPZUGKG"] = 0;
            SReturn.Merge(tmpABPOS);
            #endregion
            #region=======ZUTEI ================

            string ZUVAR = "   ";
            string GPGVAR = "   ";
            string ZULO = "  ";
            string ZULB = "   ";
            string ZULP = "   ";
            DataRow dwZUTEI = null;
            tb = tbZU.Clone();
            tb.TableName = "ZUTEI";
            var qZU = from DataRow row in tbZU.AsEnumerable()
                      where row["ZUAUFKRS"].ToString() == VKAUFKRS
                      && row["ZUAUFLFD"].ToString() == VKAUFLFD
                      && row["ZUAUFPOS"].ToString() == VKAUFPOS
                      && row["ZUPOS"].ToString() == ABAVKUPOS
                     && row["ZUSA"].ToString() == "713"
                      select row;
            foreach (DataRow row in qZU)
            {

                row["ZUAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ZUAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ZUAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ZUPOS"] = "01";
                ZUVAR = row["ZUVAR"].ToString();
                GPGVAR = row["GPGVAR"].ToString();
                ZULO = row["ZULO"].ToString();
                ZULB = row["ZULB"].ToString();
                dwZUTEI = tb.Rows.Add();//转移留位
                //dwZUTEI = tb.NewRow();//不转移留位
                dwZUTEI.ItemArray = row.ItemArray;
            }

            SReturn.Merge(tb);
            dwABPN["ABPVAR"] = GPGVAR;
            if (dwZUTEI == null)
            {
                if (!IsRepaire)
                {
                    throw new Exception("主生产单未完成留位，禁止转换");
                }
            }

            #endregion
            #region=======ICC_ABAX ================
            var qMU = from DataRow row in tbMU.AsEnumerable()
                      where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                      select row;
            foreach (DataRow row in qMU)
            {
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ABAQTYRESERVE"] = dwABA["ABARESMG"];
                row["ABAVAR"] = dwABPN["ABPVAR"];
            }
            tb = qMU.CopyToDataTable();
            tb.TableName = "ICC_ABAX";
            SReturn.Merge(tb);
            #endregion

            #region======Material===========
            DataRow dwABAN = tbMU.Rows.Add();
            dwABAN["ABAID"] = _ABAID++;
            dwABAN["ABAMANDANT"] = dwABPN["ABPMANDANT"];
            dwABAN["ABAAUFTYP"] = "N";
            dwABAN["ABAAUFKRS"] = "00";
            dwABAN["ABAAUFLFD"] = "000000";
            dwABAN["ABAAUFPOS"] = "000";
            dwABAN["ABAVKAUFTYP"] = "V";
            dwABAN["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
            dwABAN["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
            dwABAN["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
            dwABAN["ABAVKUPOS"] = "01";
            if (dwABAN["ABAVKUPOS"].ToString().Trim() == "00")
            {
                dwABAN["ABAVKUPOS"] = "01";
            }
            double ABAQTYRECEIVED = Convert.ToDouble(dwABA["ABAQTYRECEIVED"].ToString());
            double ABARESMG = Convert.ToDouble(dwABA["ABARESMG"].ToString());
            dwABAN["ABAAUFMSL"] = dwABA["ABAAUFMSL"]; //GAAUFMSL
            dwABAN["ABAPACKTYP"] = dwABA["ABAPACKTYP"];
            dwABAN["ABAHUELSE"] = dwABA["ABAHUELSE"];
            dwABAN["ABARESMG"] = dwABA["ABARESMG"];
            dwABAN["ABASANZSTK"] = "1";
            dwABAN["ABASSTKGEW"] = dwABA["ABARESMG"];
            dwABAN["ABAWAKZ"] = dwABA["ABAWAKZ"];
            dwABAN["ABAART"] = dwABA["ABAART"];
            dwABAN["ABAVAR"] = dwABPN["ABPVAR"];
            dwABAN["ABAFARBE"] = dwABA["ABAFARBE"];
            if (IsRepaire)
            {
                dwABAN["ABAART"] = Helper.Left("HUITIAO".PadRight(20), 20);
                dwABAN["ABAVAR"] = "   ";
                dwABAN["ABAFARBE"] = Helper.Left("000000".PadRight(10), 10);
            }
            dwABAN["ABACOLORREF"] = "          ";
            dwABAN["ABAVKSTARTD"] = dwABA["ABAVKSTARTD"];
            dwABAN["ABAVKART"] = dwABAN["ABASSTKGEW"];
            dwABAN["ABAQTYALLOCATE"] = ABARESMG - ABAQTYRECEIVED;
            dwABAN["ABAQTYRECEIVED"] = ABAQTYRECEIVED;
            dwABAN["ABADISPOKZ"] = "B";
            dwABAN["ABAVKFARBE"] = 100000;
            dwABAN["ABALO"] = dwABA["ABALO"];
            dwABAN["ABALB"] = dwABA["ABALB"];
            dwABAN["ABALP"] = dwABA["ABALP"];
            dwABAN["ABAUSER"] = app.User;
            dwABAN["ABADATE"] = date;
            string filter = "ABAID='" + dwABAN["ABAID"].ToString() + "'";
            DataRow[] rows = tbMU.Select(filter);
            tb = rows.CopyToDataTable();
            tb.TableName = "ICC_ABAX";
            SReturn.Merge(tb);
            #endregion
            return SReturn;
        }
        /// <summary>
        /// 仅仅创建一个MU的AB50，不处理原料关系
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dwABA"></param>
        /// <param name="_ABAID"></param>
        /// <returns></returns>
        public DataSet PlanAB50MU(DataSet src, DataRow dwABA, ref int _ABAID)
        {
            string DEND = "0"; int DAYS = 0;
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = src.Tables["ABPOS"].Clone();
            string ENDD = "0";
            string STARTD = "0";
            string ART = "", VAR = "", FARBE = "", WAKZ = "";
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataTable tb;
            decimal RQTY = 0;
            string VKAUFKRS = "", VKAUFLFD = "", VKAUFPOS = "", ABAVKUPOS = "";
            DataTable tbMU = src.Tables["ICC_ABAX"];
            DataTable tbZU = src.Tables["ZUTEI_ITX"];

            #region========Prepare=================
            VKAUFKRS = dwABA["ABAVKAUFKRS"].ToString();
            VKAUFLFD = dwABA["ABAVKAUFLFD"].ToString();
            VKAUFPOS = dwABA["ABAVKAUFPOS"].ToString();
            ABAVKUPOS = dwABA["ABAVKUPOS"].ToString();
            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString();
            APZANRKRS = "LS";
            APZALFDNR = "000001";
            RQTY = Convert.ToDecimal(dwABA["ABARESMG"].ToString());
            if (DEND == "0")
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DEND;
            }
            STARTD = Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - DAYS));
            if (STARTD.Trim() == "") STARTD = "0";
            #endregion

            #region=======dwABPN==========
            dwABPN = tmpABPOS.Rows.Add();
            dwABPN["ABPMANDANT"] = app.Mandant;
            dwABPN["ABPAUFART"] = "GP";
            dwABPN["ABPAUFKRS"] = VKAUFKRS;
            dwABPN["ABPAUFLFD"] = VKAUFLFD;
            dwABPN["ABPAUFPOS"] = VKAUFPOS;
            dwABPN["ABPLTVORL"] = dwABA["EXART"];
            dwABPN["ABPZLO"] = dwABA["ABALO"];
            dwABPN["ABPZLB"] = dwABA["ABALB"];
            dwABPN["ABPBLO"] = dwABA["ABALO"];
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPPOSTYP"] = "MU ";
            dwABPN["ABPWAKZ"] = "G";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPVTDATE"] = ENDD;
            dwABPN["ABPENDDAT"] = ENDD;
            dwABPN["ABPENDDATA"] = ENDD;
            dwABPN["ABPLIEFDATN"] = ENDD;
            dwABPN["ABPTERMDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPSTARTDATA"] = STARTD;
            dwABPN["ABPSTARTDATN"] = STARTD;
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPSOLLMENGE1"] = 0;
            dwABPN["ABPSOLLMENGE2"] = 0;
            dwABPN["ABPSOLLMENGE3"] = 0;
            dwABPN["ABPSOLLMENGE4"] = 0;
            dwABPN["ABPSOLLMENGE5"] = RQTY / 10;
            dwABPN["ABPSOLLMENGE6"] = 0;
            dwABPN["ABPSOLLMENGE7"] = 0;
            dwABPN["ABPSOLLMENGE8"] = 0;
            dwABPN["ABPZUGKG"] = RQTY / 10;
            SReturn.Merge(tmpABPOS);
            #endregion
            #region=======ZUTEI ================

            string ZUVAR = "   ";
            string GPGVAR = "   ";
            string ZULO = "  ";
            string ZULB = "   ";
            string ZULP = "   ";
            DataRow dwZUTEI = null;
            tb = tbZU.Clone();
            tb.TableName = "ZUTEI";
            var qZU = from DataRow row in tbZU.AsEnumerable()
                      where row["ZUAUFKRS"].ToString() == VKAUFKRS
                      && row["ZUAUFLFD"].ToString() == VKAUFLFD
                      && row["ZUAUFPOS"].ToString() == VKAUFPOS
                      && row["ZUPOS"].ToString() == ABAVKUPOS
                     && row["ZUSA"].ToString() == "713"
                      select row;
            foreach (DataRow row in qZU)
            {

                row["ZUAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ZUAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ZUAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ZUPOS"] = "01";
                ZUVAR = row["ZUVAR"].ToString();
                GPGVAR = row["GPGVAR"].ToString();
                ZULO = row["ZULO"].ToString();
                ZULB = row["ZULB"].ToString();
                dwZUTEI = tb.Rows.Add();
                dwZUTEI.ItemArray = row.ItemArray;
            }

            SReturn.Merge(tb);
            dwABPN["ABPVAR"] = GPGVAR;
            if (dwZUTEI == null)
                throw new Exception("主生产单未完成留位，禁止转换");

            #endregion
            #region=======ICC_ABAX ================
            /*
            var qMU = from DataRow row in tbMU.AsEnumerable()
                      where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                      select row;
            foreach (DataRow row in qMU)
            {
               // row["ABAAUFTYP"] = "V";
               // row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
               // row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                //row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                //row["ABAQTYRESERVE"] = dwABA["ABARESMG"];
                //row["ABAVAR"] = dwABPN["ABPVAR"];
            }
            tb = qMU.CopyToDataTable();
            tb.TableName = "ICC_ABAX";
            SReturn.Merge(tb);*/
            #endregion

            #region======Material===========
            DataRow dwABAN = tbMU.Rows.Add();
            dwABAN["ABAID"] = _ABAID++;
            dwABAN["ABAMANDANT"] = dwABPN["ABPMANDANT"];
            dwABAN["ABAAUFTYP"] = "N";
            dwABAN["ABAAUFKRS"] = "00";
            dwABAN["ABAAUFLFD"] = "000000";
            dwABAN["ABAAUFPOS"] = "000";
            dwABAN["ABAVKAUFTYP"] = "V";
            dwABAN["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
            dwABAN["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
            dwABAN["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
            dwABAN["ABAVKUPOS"] = "01";
            if (dwABAN["ABAVKUPOS"].ToString().Trim() == "00")
            {
                dwABAN["ABAVKUPOS"] = "01";
            }
            double ABAQTYRECEIVED = Convert.ToDouble(dwABA["ABAQTYRECEIVED"].ToString());
            double ABARESMG = Convert.ToDouble(dwABA["ABARESMG"].ToString());
            dwABAN["ABAAUFMSL"] = dwABA["ABAAUFMSL"]; //GAAUFMSL
            dwABAN["ABAPACKTYP"] = dwABA["ABAPACKTYP"];
            dwABAN["ABAHUELSE"] = dwABA["ABAHUELSE"];
            dwABAN["ABARESMG"] = dwABA["ABARESMG"];
            dwABAN["ABASANZSTK"] = "1";
            dwABAN["ABASSTKGEW"] = dwABA["ABARESMG"];
            dwABAN["ABAWAKZ"] = dwABA["ABAWAKZ"];
            dwABAN["ABAART"] = dwABA["ABAART"];
            dwABAN["ABAVAR"] = dwABPN["ABPVAR"];
            dwABAN["ABAFARBE"] = dwABA["ABAFARBE"];
            dwABAN["ABACOLORREF"] = "          ";
            dwABAN["ABAVKSTARTD"] = dwABA["ABAVKSTARTD"];
            dwABAN["ABAVKART"] = dwABAN["ABASSTKGEW"];
            dwABAN["ABAQTYALLOCATE"] = ABARESMG - ABAQTYRECEIVED;
            dwABAN["ABAQTYRECEIVED"] = ABAQTYRECEIVED;
            dwABAN["ABADISPOKZ"] = "B";
            dwABAN["ABAVKFARBE"] = 100000;
            dwABAN["ABALO"] = dwABA["ABALO"];
            dwABAN["ABALB"] = dwABA["ABALB"];
            dwABAN["ABALP"] = dwABA["ABALP"];
            dwABAN["ABAUSER"] = app.User;
            dwABAN["ABADATE"] = date;
            string filter = "ABAID='" + dwABAN["ABAID"].ToString() + "'";
            DataRow[] rows = tbMU.Select(filter);
            tb = rows.CopyToDataTable();
            tb.TableName = "ICC_ABAX";
            SReturn.Merge(tb);
            #endregion
            return SReturn;
        }
    }
}

